//flag 1 不需要解码
function getCookie(sName,flag)
{
	sName = "d_"+sName;
	var aCookie = document.cookie.split("; ");
	var v_name = "";
	for(var i=0;i<aCookie.length;i++)
	{
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
		{		
			if(flag==undefined){
				v_name = "";
				var v= unescape(aCrumb[1]).split('%');
				if(v.length>1){
					for(i=0;i<v.length;i++){
						if(v[i]>0) v_name +=String.fromCharCode(v[i]);
					}
				}
				return v_name;
			}
			return aCrumb[1];
		}
	}
	return "";
}

//收藏夹
function AddFavorite(sURL, sTitle)
{
    try{
        window.external.addFavorite(sURL, sTitle);
    }catch (e){
        try{
            window.sidebar.addPanel(sTitle, sURL,"");
        }catch (e){
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

function checkLogin()
{
	if(isLogin()){
		var user_name = getCookie('unicodeUsername');
		var login_str = '<div class="right"><a href="/mypage.php"><img src="/images/header_mypage_off.gif" title="我的主页" alt="我的主页" /></a><a href="/logout.php"><img src="/images/header_logout_off.gif" title="退出"  alt="退出"/></a><a href=javascript:AddFavorite("http://www.87dianping.com","百企点评");><img src="/images/header_col_off.gif" title="收藏本站" /></a></div><div class="login"><span>'+user_name+',欢迎您！</span></div>';
		$('#check_login').html(login_str);
	}
}

function isLogin()
{
	user_id = getCookie('id','1');
	
	if(user_id != ""){
		return true;
	}else{
		return false;
	}
}

function my_del(doUrl, id, obj)
{
	if(confirm("确认删除？")){
		$.ajax({
			type:"POST",
			url:doUrl+"?method=del&id="+id,
			success:function(msg){
				if(msg == 'ok'){
					obj.parentNode.parentNode.style.display = "none";
				}
			}            
		 });
	}
}

//判断正整数
function checkNum(num)
{
	var reg = /^[0-9]+$/;
	if(reg.test(num)){
		return true;
	}else{
		return false;
	}
}

//在文本框中插入文字
function insertText(obj,str) {
	if (document.selection) {
        var sel = document.selection.createRange();
        sel.text = str;
    } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
        var startPos = obj.selectionStart,
            endPos = obj.selectionEnd,
            cursorPos = startPos,
            tmpStr = obj.value;
        obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
        cursorPos += str.length;
        obj.selectionStart = obj.selectionEnd = cursorPos;
    } else {
        obj.value += str;
    }
}
//弹出登录界面
function go_to_login(e)
{
	var mousePosition = getMousePoint(e);
	var y = mousePosition.y-e.clientY+parseInt(document.documentElement.clientHeight / 3);
	$("#inner_login_pop").css("top",y+"px");
	$("#inner_loginbg").show();
	$("#inner_login_pop").show();
}
//关闭登录界面
$(document).ready(function(){
	$("#inner_login_close").click(function(){
		$("#inner_loginbg").hide();
		$("#inner_login_pop").hide();
	});

	$("#inner_btn2").click(function(){
		if(check_innerLogin()){
			var user_name = encodeURIComponent($("#inner_user_name").val());
			var password = $("#inner_password").val();
			var word = $("#inner_word").val();
			$.ajax({
				type:"POST",
				url:"/login.php?cmd=ajaxLogin"+"&user_name="+user_name+"&password="+password+"&word="+word,
				success:function(msg){
					var r_arr = msg.split("#");
					if(r_arr[0] == '1'){
						alert(r_arr[1]);
						window.location.reload();
					}else{
						alert(r_arr[1]);
						return false;
					}
				}            
			 });
		}
	})

	function check_innerLogin(){
		if($("#inner_user_name").val() == ""){
			alert("请输入会员ID或邮箱地址！");
			return false;
		}
		if($("#inner_password").val() == ""){
			alert("请输入密码！");
			return false;
		}
		if($("#inner_word").val() == ""){
			alert("请输入验证码！");
			return false;
		}
		return true;
	}
});

function myBrowser()
{
	var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
	var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
	var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ; //判断是否IE浏览器 
	var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox浏览器
	var isSafari = userAgent.indexOf("Safari") > -1 ; //判断是否Safari浏览器

	if(isIE){ 
	   var IE5 = IE55 = IE6 = IE7 = IE8 = false;
	   var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
	   reIE.test(userAgent);
	   var fIEVersion = parseFloat(RegExp["$1"]);

	   IE55 = fIEVersion == 5.5 ;
	   IE6 = fIEVersion == 6.0 ;
	   IE7 = fIEVersion == 7.0 ;
	   IE8 = fIEVersion == 8.0 ;

	   if(IE55){ return "IE55"; }
	   if(IE6){ return "IE6"; }
	   if(IE7){ return "IE7"; }
	   if(IE8){ return "IE8"; }
	}//isIE end

	if(isFF){ return "FF"; }
	if(isOpera){ return "Opera"; }

}

function checkAll(obj){
	var objs = $("input[name='selectId[]']");
	if(obj.checked){
		objs.each(function(){
			$(this).attr("checked",true);
		})
	}else{
		objs.each(function(){
			$(this).attr("checked",false);
		})
	}
}

function checkOne()
{
	var objs = $("input[name='selectId[]']");
	var flag = true;
	objs.each(function(){
		if($(this).attr("checked") == false){
			flag = false;return false;
		}
	})
	if(flag){
		$("#checkall").attr("checked",true);
	}else{
		$("#checkall").attr("checked",false);
	}
}
function getMousePoint(ev) {
	// 定义鼠标在视窗中的位置
	var point = {
		x:0,
		y:0
	};
 
	// 如果浏览器支持 pageYOffset, 通过 pageXOffset 和 pageYOffset 获取页面和视窗之间的距离
	if(typeof window.pageYOffset != 'undefined') {
		point.x = window.pageXOffset;
		point.y = window.pageYOffset;
	}
	// 如果浏览器支持 compatMode, 并且指定了 DOCTYPE, 通过 documentElement 获取滚动距离作为页面和视窗间的距离
	// IE 中, 当页面指定 DOCTYPE, compatMode 的值是 CSS1Compat, 否则 compatMode 的值是 BackCompat
	else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
		point.x = document.documentElement.scrollLeft;
		point.y = document.documentElement.scrollTop;
	}
	// 如果浏览器支持 document.body, 可以通过 document.body 来获取滚动高度
	else if(typeof document.body != 'undefined') {
		point.x = document.body.scrollLeft;
		point.y = document.body.scrollTop;
	}
 
	// 加上鼠标在视窗中的位置
	point.x += ev.clientX;
	point.y += ev.clientY;
 
	// 返回鼠标在视窗中的位置
	return point;
} 

function center(showObj, bgObj, e){
	var clientWidth = document.body.clientWidth;
	var objWidth = showObj.width();
	var left = parseInt(clientWidth/2)-parseInt(objWidth/2);
	showObj.css("left",left).css("top",e.pageY).show();
	var screenHeight = document.documentElement.clientHeight;
	var bodyHeight = document.body.clientHeight;
	var bgHeight = bodyHeight <= screenHeight ? screenHeight : bodyHeight;
	bgObj.css("width",(document.body.clientWidth-1)+"px").css("height",bgHeight+"px").show();
}

function smartRollover() {   
    if(document.getElementsByTagName) {   
        var images = document.getElementsByTagName("img");   
        for(var i=0; i < images.length; i++) {   
            if(images[i].getAttribute("src").match("_off."))   
            {   
                images[i].onmouseover = function() {   
                    this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));   
                }   
                images[i].onmouseout = function() {   
                    this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));   
                }   
            }   
        }   
    }   
}   
if(window.addEventListener) {   
    window.addEventListener("load", smartRollover, false);   
}   
else if(window.attachEvent) {   
    window.attachEvent("onload", smartRollover);   
}